package com.hk.demo.player.listpreview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.hk.demo.player.R;
import com.hk.player.AbsPlayerView;
import com.hk.player.PlayerControlView;
import com.hk.player.PlayerView;
import com.hk.player.core.IHKPlayerView;
import com.hk.player.core.MonitorMediaData;
import com.zhoug.common.adapters.recycler2.BaseRecyclerViewAdapter;
import com.zhoug.common.adapters.recycler2.BaseViewHolder;
import com.zhoug.common.utils.LogUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: zhoug
 * @since: 2025-08-07
 */
@Deprecated
public class PlayerListAdapter extends BaseRecyclerViewAdapter<MonitorMediaData> {
    private static final String TAG = ">>>PlayerListAdapter";
    private final LayoutInflater mLayoutInflater;
    private PlayerView mCurrentPlayerView;

    public PlayerListAdapter(Context context) {
        super(context);
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.player_list_item;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_DATA) {
            View root = mLayoutInflater.inflate(getLayoutId(viewType), parent, false);
            return new MyViewHolder(root, this);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    protected void onBindData(BaseViewHolder holder, MonitorMediaData data, int position, int viewType) {
        if (data != null) {
            if (holder instanceof MyViewHolder viewHolder) {
                PlayerView playerView = viewHolder.playerView;
                PlayerControlView playerControlView = viewHolder.playerControlView;
                playerView.setMonitorData(data);
//                viewHolder.playerControlView.setVisibility(View.GONE);
                playerView.setOnPlayBtnClickListener(onPlayBtnClickListener);

                playerControlView.setFullScreenBtnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }


        }
    }


    public AbsPlayerView.OnPlayBtnClickListener onPlayBtnClickListener=new IHKPlayerView.OnPlayBtnClickListener() {
        @Override
        public void onPlayBtnClick(IHKPlayerView playerView, boolean startPlay) {
            if(mCurrentPlayerView!=null && mCurrentPlayerView!=playerView){
                mCurrentPlayerView.stopPlay();
            }
            mCurrentPlayerView= (PlayerView) playerView;
        }
    };

    public static class MyViewHolder extends BaseViewHolder {
        private PlayerView playerView;
        private PlayerControlView playerControlView;
        private FrameLayout containerVideo;

        public MyViewHolder(View itemView, BaseRecyclerViewAdapter<?> adapter) {
            super(itemView, adapter);
            containerVideo = itemView.findViewById(R.id.containerVideo);
            playerView = itemView.findViewById(R.id.playerView);
            playerControlView = itemView.findViewById(R.id.playerControlView);

        }
    }

    /**
     * 当 单个 Item 的视图 离开屏幕或被移除时触发（例如：滑动导致视图滚出屏幕、删除 Item、数据更新导致布局变化等）。
     * @param holder Holder of the view being detached
     */
    @Override
    public void onViewDetachedFromWindow(@NonNull BaseViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        LogUtils.d(TAG, "onViewDetachedFromWindow:itemView 从窗口分离");
        if(holder instanceof MyViewHolder myViewHolder){
            myViewHolder.playerView.releasePlayer();
        }
    }

    /**
     * 当 整个 Adapter 与 RecyclerView 解除绑定 时触发（例如：调用 recyclerView.setAdapter(null)、Activity/Fragment 销毁、RecyclerView 被移除时）。
     * @param recyclerView The RecyclerView instance which stopped observing this adapter.
     */
    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
    }

}
